package com.example.tests;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Random;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.fw.ApplicationManager;
import com.thoughtworks.xstream.XStream;


public class ContactDataGenerator {
        

        protected static ApplicationManager app;

        public static void main(String[] args) throws IOException {
                String configFile = System.getProperty("configFile", "application.properties");
                Properties properties = new Properties();
                properties.load(new FileReader(new File(configFile)));
                app = new ApplicationManager(properties);
                
                app.getContactHelper().addContactPage();
                
                if (args.length < 3) {
                        System.out.println("Please specify parameters: <amount of test data> <file> <format>");
                        return;
                }
         
                int amount = Integer.parseInt(args[0]);
                File file = new File(args[1]);
                String format = args[2];
                
                if (file.exists()) {
                        System.out.println("File exists, please remote it manually:" + file);
                        return;
                }
                
                List<ContactData> contacts = generateRandomContacts(amount); 
                if ("csv".equals(format)) {
                        saveContactsToCsvFile(contacts, file);
                }else if ("xml".equals(format)) {
                        saveContactsToXmlFile(contacts, file);  
                }else {
                        System.out.println("Unknown format" + format);
                        return; 
                }
        }

        private static void saveContactsToXmlFile(List<ContactData> contacts, File file) throws IOException {
                XStream xstream = new XStream();
                xstream.alias("contact", ContactData.class);
                String xml = xstream.toXML(contacts);
                FileWriter writer = new FileWriter(file);
                writer.write(xml);
                writer.close();
        }

        
        public static List<ContactData> loadContactsFromXmlFile(File file) {
                XStream xstream = new XStream();
                xstream.alias("contact", ContactData.class);
                return (List<ContactData>) xstream.fromXML(file);
                
        }
        private static void saveContactsToCsvFile(List<ContactData> contacts, File file) throws IOException {
                FileWriter writer = new FileWriter(file);
                for (ContactData contact : contacts) {
                        writer.write(contact.getFirstName() + "," + contact.getLastName() + "," + contact.getAdressFirst() +
                                        "," + contact.getHomeTel()      + "," + contact.getMobileTel() + "," + contact.getWorkTel() 
                                        + "," + contact.getEmailFirst() + "," + contact.getEmailSec() + "," 
                                        + contact.getBday() + "," + contact.getBmonth() + "," + contact.getByear() + "," + contact.getContactGroup() + "," + contact.getAdressSec()
                                        + "," + contact.getHomeSec() + ",!" + "\n");
                }
                writer.close();
                
        }

        public static List<ContactData> loadContactsFromCsvFile(File file) throws IOException {
                List<ContactData> list = new ArrayList<ContactData>();
                FileReader reader = new FileReader(file);
                BufferedReader bufferedReader = new BufferedReader(reader);
                String line = bufferedReader.readLine();
                while (line != null) {
                        String[] part = line.split(",");
                        ContactData contact = new ContactData()
                                        .withFirstName(part[0])
                                        .withLastName(part[1])
                                        .withAdressFirst(part[2])
                                .withHomeTel(part[3])
                                .withMobileTel(part[4])
                                .withWorkTel(part[5])
                                .withEmailFirst(part[6])
                                .withEmailSec(part[7])
                                .withBday(part[8])
                                .withBmonth(Integer.parseInt(part[9]))
                                .withByear(part[10])
                                .withContactGroup(part[11])
                                .withAdressSec(part[12])
                                .withHomeSec(part[13]);
                        list.add(contact);
                        line = bufferedReader.readLine();
                        
                }
                bufferedReader.close();
                return list;
        }

        public static List<ContactData> generateRandomContacts(int amount) {
                
                List<ContactData> list = new ArrayList<ContactData>();
                for(int i = 0; i < amount; i++) {
                ContactData contact = new ContactData()
                .withFirstName(GenerateContactRandomString())  
                .withLastName(GenerateContactRandomString())  
                .withAdressFirst(GenerateContactRandomString())
                .withHomeTel(GenerateContactRandomString())   
                .withMobileTel(GenerateContactRandomString()) 
                .withWorkTel(GenerateContactRandomString())   
                .withEmailFirst(GenerateContactRandomString())
                .withEmailSec(GenerateContactRandomString())  
                .withBday(GenerateContactRandomDay())      
                .withBmonth(GenerateContactRandomMonth())    
                .withByear(GenerateContactRandomYear())     
                .withContactGroup(selectRandomGroup())
                .withAdressSec(GenerateContactRandomString()) 
                .withHomeSec(GenerateContactRandomString()); 
                list.add(contact);
                }
                return list;
        }
        
        private static String selectRandomGroup() {

                List<WebElement> options = app.driver.findElements(By.xpath("//form/select[3]/option"));
        Random rnd = new Random();
        return options.get(rnd.nextInt(options.size())).getText();

    }

        public static String GenerateContactRandomYear() {
                Random rnd = new Random();

        if (rnd.nextInt(3) == 0) {

                return "-";

        } else {

                return Integer.toString(rnd.nextInt(9998)+1);
        }
        }
        
        public static String GenerateContactRandomDay() {
                Random rnd = new Random();

        if (rnd.nextInt(3) == 0) {

                return "-";

        } else {

                return Integer.toString(rnd.nextInt(30)+1) ;

        }

        }
        public static int GenerateContactRandomMonth() {
                Random rnd = new Random();
                int monthIndex = rnd.nextInt(12);
        return monthIndex;
     }
        public static String GenerateContactRandomString() {
                Random rnd = new Random();
                if (rnd.nextInt(5) == 0) {
                        return "";      
                }else {
                        return "test" + rnd.nextInt(); 
                }
                
        }
}
